-- LaijiaGrave
-- Created by chengb Aug/2/2016
-- 莱迦之墓

-- 初始化
function initLaijiaGrave(self)
    local class = self.gridData.class;
    local buildingInfo = BuildingBonusM.queryBuildingView(class);

    if not buildingInfo then
        error("获取不到建筑查看配置，元素类型：" .. class);
        return;
    end

    local function onClicked(sender, eventType)
        if eventType == ccui.TouchEventType.began then
            if self.gridData.state == GRID_STATE_DISABLE then
                -- 已经失效，不处理
                return;
            end

            -- 获取小男孩事件进度
            local progress = BraveVSDragonM.getLittleBoyProgress();
            if progress <= 1 then
                -- 还未完成小男孩的任务
                AudioM.playFx("ui_open1");
                self:openBuildingView(nil, nil, buildingInfo["dialog"], getLocStr("btn_text_leave"));
            elseif not BraveVSDragonM.hasGotBuildingBonus(self.gridData.class) then
                -- 还没领取奖励
                local function okFunc()
                    DungeonActionM.go("brave_building_bonus", self.gridData:getPos());
                end
                AudioM.playFx("ui_open1");
                self:openBuildingView2(okFunc, nil,  buildingInfo["view_text"],  getLocStr("btn_text_leave"),
                    buildingInfo["dialog2"],  nil, true, buildingInfo["name"]);
            else
                AudioM.playFx("ui_open1");
                self:openBuildingView(nil, nil, buildingInfo["dialog"], getLocStr("btn_text_leave"));
            end
        end
    end

    local iconPath = getLevelItemIconPath(buildingInfo["icon"]);
    self.icon = ccui.ImageView:create(iconPath);
    self.icon:setTouchEnabled(true);
    self.icon:addTouchEventListener(onClicked);
    self.item:addChild(self.icon);
    self.icon:setVisible(true);
end
